package com.sundhage.gui
{
	import flash.display.*;
	import flash.events.*;
	
	public class JSForm extends Sprite {
		// types
		public static var RADIOBUTTON:String = "radiobutton";
		public static var CHECKBOX:String = "checkbox";		
		public static var INPUTTEXT:String = "inputtext";
		
		
		public static var CLICK_OK:String = "jsform_ok";
		public static var CLICK_CANCEL:String = "jsform_cancel";
		
		private var settings:JSFormSettings;
		private var assets:JSFormAssets;
		
		private var bkg:Sprite = new Sprite();
		private var content:Sprite = new Sprite();
		private var gfxContent:Sprite = new Sprite();
		
		private var okBtn:Sprite;
		private var cancelBtn:Sprite;
		
		private var items:Array = new Array();
		
		public var xmlData:String = "I AM A BIG BAMSE-TODO :) ";
		
		private function initBkg():void {
			bkg.graphics.beginBitmapFill(assets.getBackground(), null, true, true);
			bkg.graphics.drawRoundRect(0,0,settings.width, settings.height, settings.curve, settings.curve);
			bkg.graphics.endFill();
		}
		
		private function checkCenter(e:Event):void {
			if (settings.center) {
				this.x = stage.stageWidth/2 - settings.width/2;
				this.y = stage.stageHeight/2 - settings.height/2;
			}
			if (settings.useBusy) {
				assets.getBusy().show(this);
			}
		}
		
		private function initButtons():void {
			okBtn = assets.getOkBtn();
			okBtn.buttonMode = true;
			cancelBtn = assets.getCancelBtn();
			cancelBtn.buttonMode = true;
			
			okBtn.addEventListener(MouseEvent.CLICK, okClick);
			cancelBtn.addEventListener(MouseEvent.CLICK, cancelClick);
			
			okBtn.x = settings.width-okBtn.width-settings.generalPad;
			cancelBtn.x = okBtn.x-cancelBtn.width-settings.buttonPad;
			
			okBtn.y = settings.height-okBtn.height-settings.generalPad;
			cancelBtn.y = settings.height-cancelBtn.height-settings.generalPad;
			
			addChild(cancelBtn);
			addChild(okBtn);
		}
		
		private function okClick(e:Event):void {
			dispatchEvent(new Event(CLICK_OK));
			if (settings.useBusy) {
				assets.getBusy().hide();
			}
			this.parent.removeChild(this);
			
		}
		
		private function cancelClick(e:Event):void {
			dispatchEvent(new Event(CLICK_CANCEL));
			if (settings.useBusy) {
				assets.getBusy().hide();
			}
			this.parent.removeChild(this);
			
		}
		
		public function addItem(item:*, x:int, y:int):void {
			item.x = x;
			item.y = y;
			content.addChild(item);
			items.push(item);
		}
		
		public function addGraphics(item:*, x:int, y:int):void {
			item.x = x;
			item.y = y;
			gfxContent.addChild(item);
		}
		public function getXML():String {
			var s:String = "";
			s += "<JSForm id=\""+settings.xmlId+"\">";
			for (var i:int = 0; i<items.length; i++) {
				s += items[i].getXML();
			}
			s += "</JSForm>";
			return s;
		}
		public function JSForm(settings:JSFormSettings, assets:JSFormAssets) {
			this.settings = settings;
			this.assets = assets;
			addEventListener(Event.ADDED_TO_STAGE, checkCenter);
			addChild(bkg);
			addChild(gfxContent);
			addChild(content);
			
			initBkg();
			initButtons();
		}
		
	}
}